﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TimeTracker
{
    public partial class frmNote : Form
    {
        INote note;
        IProject project;

        public frmNote()
        {
            InitializeComponent();
        }

        public frmNote(TimeTracker.IProject Project)
        {
            InitializeComponent();

            this.project = Project;

            this.Text = "Add Note to: " + Project.Name;
            comboBox_Client.Text = Project.Name;

            InitilizeForm();

        }

        private void Note_Load(object sender, EventArgs e)
        {
            InitilizeForm();
        }


        private void InitilizeForm()
        {
            comboBox_Client.DataSource = Manager.Instance.Projects;
            comboBox_Client.DisplayMember = "Name";


            if (project != null)
            {
                for (int i = 0; i < comboBox_Client.Items.Count - 1; i++)
                {
                    if (((IProject)comboBox_Client.Items[i]).Name == project.Name)
                        comboBox_Client.SelectedIndex = i;
                }
                
                comboBox_TimeEntry.Enabled = true;
                
                // Set the focus to the Note field
                textBox_Note.Focus();
            }
            else
            {
                comboBox_Client.SelectedIndex = -1;
                comboBox_Client.Text = "< Project >";
                comboBox_TimeEntry.Enabled = false;
            }
        }

        private void comboBox_Client_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_Client.SelectedItem != null)
            {
                if (typeof(IProject).IsAssignableFrom(comboBox_Client.SelectedItem.GetType()))
                {
                    IEnumerable<ISession> Sessions = ((IProject)comboBox_Client.SelectedItem).Sessions; //Manager.GetSessions((IProject)comboBox_Client.SelectedItem);
                    Sessions = Sessions.Reverse();

                    comboBox_TimeEntry.DataSource = Sessions;
                    comboBox_TimeEntry.DisplayMember = "StartTime";
                    comboBox_TimeEntry.Enabled = true;
                }
            }
        }


        private void SaveNote()
        {
            if (note == null)
            {
                Manager.Instance.AddNote((ISession)comboBox_TimeEntry.SelectedItem, DateTime.Now, textBox_Note.Text);
            }
            else
            {
                this.note.Text = textBox_Note.Text;
                this.note.Time = DateTime.Now;
            }
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            SaveNote();
        }

        public string GetStringFromStream(Stream stream)
        {
            // Create a stream reader.
            using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
            {
                // Just read to the end.
                return reader.ReadToEnd();
            }
        }

        /// <summary>
        /// Handle the cursor when a file is dragged onto the Note Box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            
            //// This is beta code I tried out to allow Drag Drop from Thunderbird, it returns the type text/x-moz-message
            //// and the data is a url to the message (e.g. imap-message://jdevries@mail.wakefly.com/INBOX#135377)

            //string[] Formats = e.Data.GetFormats(true);
            
            //foreach (string Format in Formats)
            //{
            //    //textBox_Note.AppendText(Format + "\r\n");
            //    object Data = GetStringFromStream((MemoryStream)e.Data.GetData(Format));
            //}

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
                e.Effect = DragDropEffects.None;
        }

        /// <summary>
        /// Handles the file when it is dropped onto the ListView
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_DragDrop(object sender, DragEventArgs e)
        {
            Array Data = (Array)e.Data.GetData(DataFormats.FileDrop);

            if (Data != null)
            {
                ImageList x = new ImageList();
                listView1.LargeImageList = x;
                foreach (string FileName in Data)
                {

                    ListViewItem listViewItem_File = new ListViewItem();
                    try
                    {
                        string Extension = new System.IO.FileInfo(FileName).Extension;

                        if (!x.Images.ContainsKey(Extension))
                            x.Images.Add(Extension, IconHandler.IconHandler.IconFromExtension(Extension, IconHandler.IconSize.Large));

                        listViewItem_File.ImageKey = Extension;
                        
                    }
                    catch
                    {
                        // Something went wrong obtaining the Icon?
                    }

                    listViewItem_File.Text = FileName;

                    listView1.Items.Add(listViewItem_File);
                }

            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
